package com.example.com.common.view.adapter;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CheckedTextView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 普通的没有多种类型adapter都可以继承这个adapter，只需要重写其中两个方法就可以实现功能
 *
 * @param <T>
 */
public abstract class TBaseAdapter<T> extends BaseAdapter {

    protected List<T>        list;
    protected Context        context;
    protected LayoutInflater inflater;
    private   int            layoutID;

    public TBaseAdapter(Context context, T[] ts, int layoutID) {
        super();
        this.context = context;
        this.list = new ArrayList<T>();
        this.layoutID = layoutID;
        Collections.addAll(list, ts);
        this.inflater = LayoutInflater.from(context);
    }

    public TBaseAdapter(Context context, List<T> list, int layoutID) {
        super();
        this.layoutID = layoutID;
        this.context = context;
        this.list = list;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public T getItem(int position) {
        return list.get(position);
    }

    @Override
    public int getCount() {
        if (list == null)
            return 0;
        return list.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(layoutID, parent,false);
            holder = new ViewHolder(convertView);
            initView(holder);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        setViewValue(holder, position);
        return convertView;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    /**
     * 向ViewHolder类里面添加View
     *
     * @param holder
     */
    public abstract void initView(ViewHolder holder);

    /**
     * 从ViewHolder获取对应ID的View设置其值
     *
     * @param holder
     * @param t      数据对象
     */
    public abstract void setViewValue(ViewHolder holder, int position);

    public static class ViewHolder {

        SparseArray<View> array;
        View              parent;

        public ViewHolder(View parent) {
            this.parent = parent;
            array = new SparseArray<View>();
        }

        public View getView(int id) {
            return array.get(id);
        }

        public Button getButton(int id) {
            return (Button) getView(id);
        }

        public void addView(int id) {
            array.append(id, parent.findViewById(id));
        }

        public CheckedTextView getCheckedTextView(int id) {
            return (CheckedTextView) getView(id);
        }

        public CheckBox getCheckBox(int id) {
            return (CheckBox) getView(id);
        }


        public TextView getTextView(int id) {
            return (TextView) getView(id);
        }

        public ImageView getImageView(int id) {
            return (ImageView) getView(id);
        }

        public ImageButton getImageButton(int id) {
            return (ImageButton) getView(id);
        }

        public View getParent() {
            return parent;
        }

        public void setParent(View parent) {
            this.parent = parent;
        }
    }
}
